package homework;

import java.io.IOException;
import java.net.*;

/**
 * @author DCSGO
 * @version 1.0
 * (1)编写一个接收端A，和一个发送端B，使用UDP协议完成
 * (2)接收端在8888端口等待接收数据(receive)
 * (3)发送端向接收端发送数据“四大名著是哪些？"
 * (4)接收端接收到发送端发送的问题后，返回“四大名著是<<红楼梦>>..."，否则返回 what?
 * (5)接收端和发送端程序退出
 */
public class Homework02SenderB {
    public static void main(String[] args) throws IOException {
        DatagramSocket datagramSocket = new DatagramSocket(9999);
        byte[] data = "四大名著是哪些？".getBytes();
        //byte[] data = "你的名字？".getBytes();

        DatagramPacket datagramPacket =
                new DatagramPacket(data, data.length, InetAddress.getByName("192.168.124.10"), 8888);

        datagramSocket.send(datagramPacket);
        System.out.println("问题已发送！");

        byte[] ans = new byte[1024];
        DatagramPacket receive = new DatagramPacket(ans, ans.length);
        datagramSocket.receive(receive);
        System.out.println("答案是：" + new String(ans, 0, receive.getLength()));

        datagramSocket.close();

        /*
         * 问题已发送！
         * 答案是：四大名著是<<红楼梦>>...
         *
         * 进程已结束，退出代码为 0
         */
    }
}
